package com.xywy.bloodandroid.act;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.DateUtil;
import com.xywy.bloodandroid.util.MD5;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;

public class ExpertsArticleAct extends BaseActivity {
	private RelativeLayout left_img;
	private TextView middle_title;
//	private ImageView right_img;
	private int count = 1;
	private String id;
	private String cateid;
	private WebView artitle_content;
	private TextView name;
	private TextView time;
	private TextView artical_title;
	private String document_id;
	private int data = -1;// 0是未收藏 1收藏
	private String sign;
	private String title;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_expert_articl);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
//		right_img = (ImageView) findViewById(R.id.right_img);
//		right_img.setVisibility(View.GONE);

		artical_title = (TextView) findViewById(R.id.artical_title);
		artitle_content = (WebView) findViewById(R.id.artitle_content);

		name = (TextView) findViewById(R.id.name);
		time = (TextView) findViewById(R.id.time);

		// right_img.setBackgroundResource(R.drawable.blue_star);
		middle_title.setText(R.string.artail_detail);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
//		right_img.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		document_id = getIntent().getExtras().getString("document_id", "");// 医生id
		id = getIntent().getExtras().getString("id", "");
		cateid = getIntent().getExtras().getString("cateid", "");

		try {
			sign = MD5.md5s(Constants.Sign + Constants.tag);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 获取是否收藏
		getIsCollection();

		getArtailDetail(id, cateid);
	}

	/**
	 * 获取文章是否收藏
	 */
	private void getIsCollection() {

		PostRequest<String> request = new PostRequest<String>(
				Constants.COllection_url + "&sign=" + sign, String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context,
								getResources().getString(R.string.network_slow));
						// right_img.setVisibility(View.VISIBLE);
						// right_img.setBackgroundResource(R.drawable.blue_star);
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						JSONObject jsonObject;

						try {
							jsonObject = new JSONObject(response);
							int state = jsonObject.optInt("state");
							data = jsonObject.optInt("data");// 0是未收藏 1收藏
							String error = jsonObject.optString("error");
//							right_img.setVisibility(View.VISIBLE);
							if (state == 200) {

//								if (data == 1) {
//									right_img
//											.setBackgroundResource(R.drawable.orange_star);
//								} else
//									right_img
//											.setBackgroundResource(R.drawable.blue_star);
							} else {
								UIUtil.showToast(context, error);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("categoryid", cateid);// 文章种类id
		params.put("document_id", document_id);// 医生id
		params.put("xywy_userid", User.getInstance(context).getUserid());
		request.setParams(params);
		request.setIsParseJson(false);
		executeRequest(request);

	}

	private void getArtailDetail(String id, String cateid) {
		GetRequest<String> getRequest = new GetRequest<String>(Constants.getUrl(
				"zhuanjia", "ArticleDetails")
				+ "&id="
				+ id
				+ "&cateid="
				+ cateid + "&source=cdsb", String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context,
								getResources().getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						try {
							JSONObject object = new JSONObject(response);
							int code = object.optInt("code");
							JSONObject object1 = object.optJSONObject("data");
							String error = object.optString("error");
							if (code == 10000) {
								ParseJson(object1);
							} else {
								UIUtil.showToast(context, error);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
		getRequest.setIsParseJson(false);
		executeRequest(getRequest);
	}

	protected void ParseJson(JSONObject object1) {
		String contents = object1.optString("contents");// 内容
		title = object1.optString("title");
		String addtime = object1.optString("addtime");// 创建时间
		String name1 = object1.optString("name");// 医生的名字

		artical_title.setText(title);

		name.setText(name1);
		time.setText(DateUtil.getData(addtime));

		artitle_content.loadData(contents, "text/html; charset=UTF-8", null);

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.right_img:
			// if (count % 2 == 0) {
			// right_img.setBackgroundResource(R.drawable.blue_star);
			// } else {
			// right_img.setBackgroundResource(R.drawable.orange_star);
			// }
			// count++;
          System.out.println("打印一下  收藏的data...."+data);
			if (data == 1) {
				// 点击完 是 取消收藏
				CollectionArtail("0", 1);

			} 
			if (data == 0) {
				// 收藏
				CollectionArtail("1", 0);
			}

			break;

		default:
			break;
		}
	}

	// 添加收藏
	private void CollectionArtail(String category, final int flag) {
		System.out.println("dizhi ..." + Constants.IsCOllection_url + "&sign="
				+ sign);
		PostRequest<String> request = new PostRequest<String>(
				Constants.IsCOllection_url + "&sign=" + sign, String.class,
				new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context,
								getResources().getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						JSONObject jsonObject;
						System.out.println("获取收藏的状态......" + response);
						try {
							jsonObject = new JSONObject(response);
							int state = jsonObject.optInt("state");
							boolean data1 = jsonObject.optBoolean("data");
							String error = jsonObject.optString("error");
//							right_img.setVisibility(View.VISIBLE);
							if (state == 200) {
								if (flag == 1) {
//									if (data1) {
//										right_img
//												.setBackgroundResource(R.drawable.blue_star);
//									} else
//										right_img
//												.setBackgroundResource(R.drawable.orange_star);
//									data=0;//让data的值变
//								} else if (flag == 0) {
//									if (data1) {
//										right_img
//												.setBackgroundResource(R.drawable.orange_star);
//									} else
//										right_img
//												.setBackgroundResource(R.drawable.blue_star);
//									data=1;//让data的值变
								}
							} else {
								UIUtil.showToast(context, error);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("categoryid", cateid);// 文章种类id
		params.put("document_id", document_id);// 医生id
		params.put("xywy_userid", User.getInstance(context).getUserid());

		params.put("title", title);
		params.put("category", category);
		request.setParams(params);
		request.setIsParseJson(false);
		executeRequest(request);
	}
}
